<?php
defined ( 'BASEPATH' ) or exit ( 'No direct script access allowed' );
class Purprice_model extends CI_Model {
	public function __construct() {
		parent::__construct ();
	}
	/*
	 * 添加进货价记录
	 */
	public function create($purprice) {
		$this->db->insert ( 't_purprice', $purprice);
		return $this->db->insert_id ();
	}
	/*
	 * 根据日期返回进货价记录
	 */
	public function find_by_date($date){
		$this->db->select('t_purprice.id,t_purprice.productid,t_product.name,t_purprice.purprice,t_product.unit,t_purprice.add_time');
		$this->db->from('t_purprice');
		$this->db->join('t_product', 't_product.id = t_purprice.productid','left');
		$this->db->where("TO_DAYS(add_time)-TO_DAYS('".$date."')=1");
		$query=$this->db->get();		
		return $query->result_array();
	}
	/*
	 * 根据日期和商品ID返回进货价记录
	 */
	public function find_by_date_product($date,$productid){
		$this->db->where("productid", $productid);
		$this->db->where("TO_DAYS(add_time)-TO_DAYS('".$date."')=1");
		$query=$this->db->get('t_purprice');		
		return $query->row_array();
	}
	/*
	 * 更新进货价记录
	 */
	public function update($id,$purprice){
		$this->db->where('id', $id);
		$this->db->update('t_purprice', $purprice);
		return $this->db->affected_rows()>0;
	}
}